<template>
	<view>
		<u-swipe-action v-for="item in addList" :index="item.id" :key="item.id" :options="options" :disabled="flag"
			@click="click" btn-width="250" :show="false">
			<view class="address u-p-r-30 u-flex u-row-between u-col-center">
				<view class="left" @tap="selectAdd(item.id, item.is_default)">
					<view class="add u-flex">
						<view class="u-m-r-10">
							{{item.name}} {{item.phone}}
						</view>
						<view>
							<u-button type="error" v-if="item.is_default" size="mini">默认</u-button>
						</view>
					</view>
					<view class="phone">
						{{item.city + item.county + ' ' + item.address}}
					</view>
				</view>
				<view class="right" @tap="editor(item.id)">
					<u-icon name="edit-pen" color="#999" size="28"></u-icon>
				</view>
			</view>
		</u-swipe-action>

	</view>
</template>

<script>
	export default {
		name: "add-address",
		props: {
			addList: {
				type: Array,
				default: []
			},
			address: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				flag: false,
				options: [{
						text: '设为默认',
						style: {
							backgroundColor: '#007aff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				]
			};
		},

		methods: {
			editor(id) {
				this.$u.route({
					url: '/pages/cart/add-list',
					params: {
						id
					}
				});
			},
			/**
			 * 选择地址
			 */
			async selectAdd(address, is_default) {
				if (this.address == 'pages/cart/add') {
					this.show = true;
					!is_default ? (await this.$u.api.setDefault(address)) : '';
				} else {
					// 个人中心进入地址管理
					const pages = getCurrentPages();
					console.log(pages);
				}
				setTimeout(() => {
					this.$u.route('pages/cart/view-cart')
				}, 500)
			},

			/**
			 * 
			 */
			async click(index, index1) {
				// 点击删除按钮删除该地址
				if (index1 == 1) {
					await this.$u.api.deleteAdd(index)
					this.$u.toast('删除成功');
					setTimeout(() => {
						this.$emit('getAdd')
					}, 500)
				} else {
					//设为默认
					if (index) await this.$u.api.setDefault(index)
					this.$u.toast('设为默认成功');
					setTimeout(() => {
						this.$u.route('pages/cart/add')
					}, 500)
				}
				// console.log(index, index1)
			}
		}

	}
</script>

<style lang="scss" scoped>
	.address {
		border-bottom: 1px solid #eee;

		.left {
			flex: auto;
			padding: 30rpx;

			.add {
				height: 52rpx;
			}
		}

		.phone {
			font-size: 28rpx;
			color: #999;
		}
	}

	.u-swipe-del {
		white-space: nowrap;
	}
</style>
